import $ from 'jQuery';
import URI from 'URI';
$(function () {
    let checkFileType = function(fileName){
        var index= fileName.lastIndexOf('.'); //得到"."在第几位
        var suffix=fileName.substring(index); //截断"."之前的，得到后缀
        if(suffix!=".xls"){  //根据后缀，判断是否符合xls格式
            return false;
        }
        return true;
    };
    let $vue = new Vue({
        el: '.main-hook',
        data: {
            durationType: [{id: 1, name: '库存'}, {id: 2, name: '已租'}, {id: 3, name: '已售'}],
            lifeCycleState: [{id: 1,name: '超期'}, {id: 0,name: '正常'}],
            isBad: [{id: 1,name: '是'}, {id: 0,name: '否'}],
            operationState: [{id: 1,name:'存量'},{id: 2,name: '流转'},{id:3,name: '故障'}],
            companyList: [],
             boxs: {
                 box1: {show: true, hook: '.box1-hook', lock: true, url: '', params: {}},
                 box2: {show: false, hook: '.box2-hook', url: '', params: {}},
                 box3: {show: false, hook: '.box3-hook', url: '', params: {}},
                 box4: {show: false, hook: '.box4-hook', url: '', params: {}}
             },
            info:{
                model:''
            }
        },
        methods: {
               emitPage: function () {
                   M.Page.load(this.boxs);
               },
            emitMap:function () {
                M.Page.emitPrePage();
            },
              // 上一页
              prePage: function () {
                  M.Page.prePage(this.boxs);
              },
            searchForm:function(){
                $('.search-hook').trigger('click');
            },
            // 触发刷新页(当前显示的页面)
             refreshPage: function () {
                 M.Page.refreshPage(this.boxs);
             },
            newPallet:function(){        //新建
                M.Page.emit(URI.OPERATE.SINGLELAMP.CREATE_PALLET.PAGE);
                IOT.setSessionStore(URI.OPERATE.SINGLELAMP.CREATE_PALLET.PAGE, JSON.stringify({action: ACTION.CREATE}));
            },
            searchTable: function () {
                $('.single-lamp-table-hook').bootstrapTable('onCustomSearch');
            },
            exportExcel:function(){//下载
                IOT.showOverlay('正在导出，请稍等...');
                IOT.getServerData(URI.OPERATE.SINGLELAMP.EXPORT,{}, (ret) => {
                    IOT.tips(ret.msg || '操作失败，请重试！', 'error', 1000, null);
                    IOT.hideOverlay();
                    if (ret && ret.code === 200) {
                        window.location.href = ret.data.substr(1);
                        // console.log(window.location.href);
                        IOT.tips('导出完成！', 'success');
                    } else {
                        IOT.tips(ret.message || '服务器请求失败，稍后再试！', 'error');
                    }
                });
                return false;
            },
            uploadFile:function () {//上传文件
                let params = $vue.$data.info;
                params.model =$('#model')[0].files[0];
                if(!checkFileType(params.model.name)){
                    IOT.tips('不是指定xls格式,请重新选择！', 'error', 1000, null);
                    return;
                }
                IOT.showOverlay('保存中，请稍等...');
                IOT.doFileUpload2(URI.OPERATE.SINGLELAMP.UPLOAD,params, function (ret) {
                    IOT.tips(ret.message || '操作失败，请重试！', 'error', 1000, null);
                    IOT.hideOverlay();
                    if (ret && ret.code === 200) {
                        IOT.tips('保存成功！', 'success');
                        M.Table.refresh.all();
                    } else {
                        IOT.tips(ret.message || '操作失败，请重试！', 'error', 1000, null);
                    }
                });
            }
        },
        mounted: function () {
            this.$nextTick(() => {
                var $defaultHook = $('.single-lamp-hook');
                // 初始化时间控件
                let tableHookName = '.single-lamp-table-hook';
                let $form = $defaultHook.find('.form-hook');
                // 查询
                $form.on('submit', function () {
                    $(tableHookName).bootstrapTable('onCustomSearch');
                    return false;
                });
                var $tableHook = $defaultHook.find(tableHookName).bootstrapTable({
                    scrollbar: 'single-lamp-table-body',
                    striped: true, // 设置为 true 会有隔行变色效果
                    pagination: true, // true 显示分页
                    paginationDetail: true, // 分页详情
                    sidePagination: 'server', // 服务器端
                    method: 'post',
                    url: URI.OPERATE.SINGLELAMP.LIST,
                    debug: false,
                    cache: false,
                    pageNumber: 1,
                    pageSize: 20,
                    pageList: [10, 20, 50, 100],
                    customButton: '',
                    customQueryParams: function (params) {
                        var formParams = $form.serializeJson();
                        var queryParams = $.extend({}, params, formParams);
                        return queryParams;
                    },
                    onLoadSuccess: function () {
                    },
                    onLoadError: function (status, xhr) {
                    },
                    onCheckAll: function (rows) {
                    },
                    columns: [
                        {
                            field: '', checkbox: true, align: 'center', fixedLeft: true, width: 60,
                            events: {
                                'change .checkbox-hook': function (e, value, row, index) {
                                    console.log('复选框: ' + value);
                                }
                            }
                        },
                        {
                            field: 'deviceId', title: '单灯编号', align: 'center', fixedLeft: true,width:180,tips:true
                        },
                        {
                            field: 'operationState', title: '所属项目', align: 'center', fixedLeft: true, width: 100, tips: true,
                            formatter: function (val, row, index) {
                                if(val== '1' ){
                                    return "存量"
                                }else if(val == '2' ){
                                    return "流转"
                                }else if(val == '3' ){
                                    return "故障"
                                }
                            }
                        },
                        {
                            field: 'company', title: '所属分组', align: 'center', fixedLeft: true,tips: true
                        },
                        {
                            field: 'lifeCycleRemainingTime', title: '灯杆编号', align: 'center',width:150, fixedLeft: true
                        },
                        {
                            field: 'bootInterval', title: '控制器类型', align: 'center', fixedLeft: true,width:140,tips:true,
                            formatter: function (val, row, index) {
                                if(val==""){
                                    return "-"
                                }else {
                                    return val;
                                }
                            }
                        },
                        {
                            field: 'latestReportTime', title: '盏数', align: 'center', fixedLeft: true,tips:true,width:160,
                            formatter: function (val, row, index) {
                                if(val== " " || val==null ){
                                    return "-";
                                }else {
                                    return val;
                                }
                            }
                        },
                        {
                            field: 'company', title: '状态', align: 'center', fixedLeft: true,tips: true
                        },
                        {
                            field: 'lifeCycleRemainingTime', title: '安装时间', align: 'center',width:150, fixedLeft: true
                        },
                        {
                            field: 'operate', title: '操作', align: 'center', fixedLeft: true, width: 180,
                            events: {
                                'click .detail': function (e, value, row, index) {
                                    M.Page.emit(URI.OPERATE.SINGLELAMP.DETAIL.PAGE, {
                                        deviceId: row.deviceId,
                                    });
                                    IOT.setSessionStore(URI.OPERATE.SINGLELAMP.DETAIL.PAGE, row.deviceId);
                                    return false;
                                },
                                'click .set': function (e, value, row, index) {
                                    M.Page.emit(URI.OPERATE.SINGLELAMP.SET.PAGE, {deviceId: row.deviceId});
                                    IOT.setSessionStore(URI.OPERATE.SINGLELAMP.SET.PAGE, row.deviceId);
                                    return false;
                                },
                                'click .modify': function (e, value, rowData, index) {
                                    M.Page.emit(URI.OPERATE.SINGLELAMP.CREATE_PALLET.PAGE);
                                    IOT.setSessionStore(URI.OPERATE.SINGLELAMP.CREATE_PALLET.PAGE, JSON.stringify({
                                        action: ACTION.MODIFY,
                                        deviceId: rowData.deviceId
                                    }));
                                    return false;
                                },
                                'click .delete': function (e, value, rowData, index) {
                                    layer.confirm(' &nbsp;&nbsp; &nbsp;&nbsp;是否确认删除？', {
                                        btn: ['确定', '取消'] //按钮
                                    }, function () {
                                        IOT.getServerData(URI.OPERATE.SINGLELAMP.DELETE, {deviceId:rowData.deviceId}, (ret) => {
                                            if (ret.code === 200) {
                                                layer.closeAll();
                                                M.Table.refresh.all();
                                            } else {
                                                IOT.tips(ret.message || '服务器请求失败，稍后再试！', 'error');
                                            }
                                        });
                                    }, function () {
                                        //取消
                                    });
                                    return false;
                                },
                            },
                            formatter: function (value, row, index) {
                                let html = [];
                                    html.push('<a class="detail operate">详情</a>');
                                    html.push('<a class="set    operate">设置</a>');
                                    html.push('<a class="modify operate">修改</a>');
                                    html.push('<a class="delete operate">删除</a>');
                                return html.join('');
                            }
                        }
                    ]
                });
            });
        }
    });
});